﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.UI.DataVisualization.Charting;
using System.Drawing;
using System.ComponentModel;
using System.Xml.Linq;
using Atendex.Dominio;
using Atendex.Dominio.Interfaces;
using Atendex.Infraestrutura.Repositórios;

namespace Atendex.Web
{
    public partial class RelatorioSolicitacoesRequisitadas : System.Web.UI.Page
    {
        DataTable dt;

        protected void Page_Load(object sender, EventArgs e)
        {
            dt = new DataTable();
            dt.Columns.Add(new DataColumn("TipoSolicitacao"));
            dt.Columns.Add(new DataColumn("Solicitacoes", typeof(int)));

            RepositorioTipoSolicitacaoAN repTSol = new RepositorioTipoSolicitacaoAN();
            ITipoSolicitacao[] tiposSol = repTSol.ObtemTodos();

            foreach (ITipoSolicitacao tSol in tiposSol)
            {
                RepositorioOrdemServico repOrdemServico = new RepositorioOrdemServico();                IOrdemServico[] ordemServicos = repOrdemServico.ObtemPorPropriedade("Tipo", tSol);
                if (calDataInicio.SelectedDate != DateTime.MinValue)
                {
                    if (calDataFim.SelectedDate != DateTime.MinValue)
                    {
                        var solicitacoesData = from IOrdemServico solicitacao in ordemServicos                                               where solicitacao.DataInicio >= calDataInicio.SelectedDate
                                               && solicitacao.DataInicio <= calDataFim.SelectedDate
                                               select solicitacao;
                        ordemServicos = solicitacoesData.ToArray<IOrdemServico>();                    }
                    else
                    {
                        var solicitacoesData = from IOrdemServico solicitacao in ordemServicos                                               where solicitacao.DataInicio >= calDataInicio.SelectedDate
                                               select solicitacao;
                        ordemServicos = solicitacoesData.ToArray<IOrdemServico>();                    }
                }
                else
                {
                    if (calDataFim.SelectedDate != DateTime.MinValue)
                    {
                        var solicitacoesData = from IOrdemServico solicitacao in ordemServicos                                               where solicitacao.DataInicio <= calDataFim.SelectedDate
                                               select solicitacao;
                        ordemServicos = solicitacoesData.ToArray<IOrdemServico>();                    }
                }

                int i = 0;
                DataRow dr = dt.NewRow();
                dr["TipoSolicitacao"] = tSol.Nome;

                foreach (IOrdemServico solicitacao in ordemServicos)                    dr["Solicitacoes"] = ++i;

                dt.Rows.Add(dr);

                Series serie = new Series(tSol.Nome);

                double point;
                if (!double.TryParse(dr["Solicitacoes"].ToString(), out point))
                    point = 0;
                
                serie.Points.AddY(point);
                crtGrafico.Series.Add(serie);
            }

            grvDados.DataSource = dt;
            grvDados.DataBind();
        }
    }
}
